package cn.virens.library.widget.form.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.support.v7.widget.AppCompatEditText;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;

import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cn.virens.library.R;
import cn.virens.library.uitls.CalendarUtil;
import cn.virens.library.uitls.LogUtil;
import cn.virens.library.widget.form.FormException;
import cn.virens.library.widget.form.FormInputViewInter;


/**
 * Created by virens on 2017/6/16.
 */

public class FormInputView extends AppCompatEditText implements FormInputViewInter, View.OnFocusChangeListener {
    private String name;
    private String regex;
    private String error;
    private boolean ignore;
    private boolean required;
    private OnInputListener listener;

    public FormInputView(Context context) {
        this(context, null);
    }

    public FormInputView(Context context, AttributeSet attrs) {
        this(context, attrs, android.support.v7.appcompat.R.attr.editTextStyle);
    }

    public FormInputView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this._initAttrs(attrs, defStyleAttr);

        this.setTextColor(Color.parseColor("#424242"));
        this.setBackgroundColor(Color.TRANSPARENT);
        this.setOnFocusChangeListener(this);
    }

    private void _initAttrs(AttributeSet attrs, int defStyleAttr) {
        final TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.FormInputView, defStyleAttr, 0);

        this.required = a.getBoolean(R.styleable.FormInputView_required, false);
        this.ignore = a.getBoolean(R.styleable.FormInputView_ignore, false);
        this.regex = a.getString(R.styleable.FormInputView_regex);
        this.error = a.getString(R.styleable.FormInputView_error);
        this.name = a.getString(R.styleable.FormInputView_name);

        a.recycle();
    }

    public boolean isRequired() {
        return required;
    }

    public void setRequired(boolean required) {
        this.required = required;
    }

    @Override
    public boolean isIgnore() {
        return ignore || (getVisibility() != VISIBLE);
    }

    @Override
    public void setIgnore(boolean ignore) {
        this.ignore = ignore;
    }

    public String getRegex() {
        return regex;
    }

    public void setRegex(String regex) {
        this.regex = regex;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String getValue() {
        CharSequence result = getEditableText();
        if (TextUtils.isEmpty(result)) {
            return null;
        } else {
            return result.toString();
        }
    }

    @Override
    public void setValue(Object value) {
        if (value == null) {
            this.setText("");
        } else if (value instanceof Date) {
            this.setText(CalendarUtil.format((Date) value, "yyyy-MM-dd"));
        } else if (value instanceof Calendar) {
            this.setText(CalendarUtil.format((Calendar) value, "yyyy-MM-dd"));
        } else {
            this.setText(value.toString());
        }
    }

    public OnInputListener getOnInputListener() {
        return listener;
    }

    public void setOnInputListener(OnInputListener listener) {
        this.listener = listener;
    }

    /**
     * 表单验证
     *
     * @return
     */
    @Override
    public void validate() throws FormException {
        if (isIgnore()) return;

        String _val = getValue();

        // 验证是否为空
        if (TextUtils.isEmpty(_val)) {
            if (required) throw new FormException(this, error);
            return;
        }
        // 正则式验证
        if (!TextUtils.isEmpty(regex)) {
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(_val);

            if (!matcher.matches()) throw new FormException(this, error);
            return;
        }
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus || isIgnore()) return;

        try {
            this.validate();

            if (listener != null) listener.onInput(FormInputView.this, getValue());
        } catch (FormException e) {
            LogUtil.e(e);
        }
    }

    public interface OnInputListener {
        void onInput(FormInputView view, String value);
    }
}
